package com.market.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.market.Dto.ProductQueryDto;
import com.market.Dto.ProductSaveDto;
import com.market.Vo.ProductQueryVo;
import com.market.entity.Product;
import com.market.mapper.ProductMapper;
import com.market.service.ProductService;
import com.market.utils.AliOssUtil;
import com.market.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 商品分页查询
     * @param productQueryDto
     * @return
     */
    @Override
    public ProductQueryVo list(ProductQueryDto productQueryDto) {
        PageHelper.startPage(productQueryDto.getPage(),productQueryDto.getLimit());
        Page<Product> page=productMapper.pageQuery(productQueryDto);
        ProductQueryVo productQueryVo=new ProductQueryVo();
        productQueryVo.setTotal((int)page.getTotal());
        productQueryVo.setRecords(page.getResult());

        return productQueryVo;
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @Override
    public String upload(MultipartFile file) {
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //原始文件名前后缀
            String extention = originalFilename.substring(originalFilename.lastIndexOf("."));

            //构造新文件名称
            String objectName=UUID.randomUUID().toString()+extention;

            //文件请求路径
            String filePath=aliOssUtil.upload(file.getBytes(),objectName);

            return filePath;
        } catch (IOException e) {
            log.error("文件上传失败：{}",e);
        }
        return null;
    }

    /**
     * 添加商品
     * @param productSaveDto
     * @return
     */
    @Override
    public String add(ProductSaveDto productSaveDto) {
        Product product=new Product();
        BeanUtils.copyProperties(productSaveDto,product);
        productMapper.saveProduct(product);
        return null;
    }

    /**
     * 获取商品详情
     * @param id
     * @return
     */
    @Override
    public Product getById(Long id) {
        Product product=productMapper.getById(id);
        return product;
    }

    /**
     * 修改商品
     * @param productSaveDto
     * @return
     */
    @Override
    public void update(ProductSaveDto productSaveDto) {
        Product product=new Product();
        BeanUtils.copyProperties(productSaveDto,product);
        productMapper.update(product);
    }

    /**
     * 删除商品
     * @param id
     */
    @Override
    public void delete(Long id) {
        productMapper.deleteById(id);
    }
}
